package cn.angleline.cloudhub.easbi

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.format.annotation.DateTimeFormat
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.util.*

@RestController
@RequestMapping("/report", MediaType.APPLICATION_JSON_UTF8_VALUE)
class ReportCtrl {

    @Autowired
    private lateinit var reportApiService: ReportApiService

    @Autowired
    private lateinit var paymentBillService: PaymentBillService

    /**
     * 获取token
     */
    @GetMapping("/token")
    fun getAccessToken(): String {
        return reportApiService.getAccessToken()
    }

    /**
     * 创建付款单表头
     */
    @GetMapping("/create/payment")
    fun createDataBase() {
        reportApiService.judgeNeedCreate()
    }

    /**
     * 创建收款单表头
     */
    @GetMapping("/create/receiving")
    fun createReceivingDataBase() {
        reportApiService.judgeNeedCreateReceiving()
    }

    /**
     * 创建资金预算表头
     */
    @GetMapping("/create/budget")
    fun createBudgetDataBase() {
        reportApiService.createBudgetDataBase()
    }

    /**
     * 2.上传收支汇总数据源
     */
    @GetMapping("/upload/all")
    fun uploadFoudSumData(@DateTimeFormat(pattern = "yyyy-MM") @RequestParam startTime: Date?,
                          @DateTimeFormat(pattern = "yyyy-MM") @RequestParam endTime: Date?) {
        reportApiService.uploadFundSum(startTime,endTime)
    }

    /**
     * 3.上传付款单一级明细表
     */
    @GetMapping("/upload/payment/first")
    fun uploadFirstPayment(@DateTimeFormat(pattern = "yyyy-MM") @RequestParam startTime: Date?,
                           @DateTimeFormat(pattern = "yyyy-MM") @RequestParam endTime: Date?) {
        reportApiService.uploadPaymentTypeSum(startTime,endTime)
    }

    /**
     * 4.上传付款单二级明细表
     */
    @GetMapping("/upload/payment/second")
    fun uploadSecondPayment(@DateTimeFormat(pattern = "yyyy-MM") @RequestParam startTime: Date?,
                            @DateTimeFormat(pattern = "yyyy-MM") @RequestParam endTime: Date?) {
        reportApiService.uploadPaymentSum(startTime,endTime)
    }


    /**
     * 上传付款单单据数据源
     */
    @GetMapping("/upload/payment")
    fun uploadData() {
        reportApiService.uploadPaymentTableInfo()
    }

    /**
     * 上传收款单单据数据源
     */
    @GetMapping("/upload/receiving")
    fun uploadReceivingData() {
        reportApiService.uploadReceivingTableInfo()
    }

    /**
     * 5.上传收款单单据汇总数据源
     */
    @GetMapping("/upload/receiving/sum")
    fun uploadReceivingSumData(@DateTimeFormat(pattern = "yyyy-MM") @RequestParam startTime: Date?,
                               @DateTimeFormat(pattern = "yyyy-MM") @RequestParam endTime: Date?) {
        reportApiService.uploadReceivingSumData(startTime,endTime)
    }


    /**
     * 删除付款单数据源
     */
    /*@GetMapping("/delete/payment")
    fun deletePaymentDataSource() {
        reportApiService.deleteDataSource("5b5ed1f85ce367135e59f5c9")
    }*/

    /**
     * 删除收款单数据源
     */
    /*@GetMapping("/delete/receiving")
    fun deleteReceivingDataSource() {
        reportApiService.deleteDataSource("5b5edb9e5ce367135e59f66e")
    }*/

    @GetMapping("/admin/info")
    fun getAdminInfo() {
        reportApiService.getAdminTableInfo()
    }

    /**
     * 6.资金预算实际数
     */
    @GetMapping("/upload/budget")
    fun exportExcel(@DateTimeFormat(pattern = "yyyy-MM") @RequestParam startTime: Date?,
                    @DateTimeFormat(pattern = "yyyy-MM") @RequestParam endTime: Date?) {
        reportApiService.getBudget(startTime,endTime)
    }

    @GetMapping("/item")
    fun testItem(@DateTimeFormat(pattern = "yyyy-MM") @RequestParam(value = "start_time") startTime: Date,
                 @DateTimeFormat(pattern = "yyyy-MM") @RequestParam(value = "end_time") endTime: Date) {
        paymentBillService.getDateYear(startTime, endTime)
    }


}